﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;

namespace Common
{
    public class LocalIP
    {
        /// <summary>
        /// 广播指定地址
        /// </summary>
        public const string broadcastIpstring = "192.168.0.255";
        /// <summary>
        /// 广播端口号
        /// </summary>
        public const int broadcastPort = 7788;

        /// <summary>
        /// 获取本机IPV4地址
        /// </summary>
        /// <returns>本机IP地址</returns>
        public static string GetLocalIPV4()
        {
            string ip = string.Empty;
            try
            {
                //string HostName = System.Net.Dns.GetHostName(); //得到主机名
                //System.Net.IPHostEntry IpEntry = System.Net.Dns.GetHostEntry(HostName);
                //for (int i = 0; i < IpEntry.AddressList.Length; i++)
                //{
                //    //从IP地址列表中筛选出IPv4类型的IP地址
                //    //AddressFamily.InterNetwork表示此IP为IPv4,
                //    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                //    if (IpEntry.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                //    {
                //        ip = IpEntry.AddressList[i].ToString();
                //    }
                //}
                //return ip;



                //NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
                //int len = interfaces.Length;
                //string mip=string.Empty;

                //for (int i = 0; i < len; i++)
                //{
                //    NetworkInterface ni = interfaces[i];

                //    if (ni.Name.Equals("以太网") || ni.Name.Equals("本地连接"))
                //    {
                //        IPInterfaceProperties property = ni.GetIPProperties();
                //        foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                //        {
                //            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                //            {
                //                mip = ip.Address.ToString();
                //            }
                //        }
                //    }
                //}
                //return mip;

                NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in fNetworkInterfaces)
                {
                    string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
                    Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                    if (rk != null)
                    {

                        // 区分 PnpInstanceID   

                        // 如果前面有 PCI 就是本机的真实网卡  

                        // MediaSubType 为 01 则是常见网卡，02为无线网卡。  

                        string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();

                        int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));

                        if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
                        {
                            IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();

                            UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;

                            foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                            {
                                if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                                    ip = UnicastIPAddressInformation.Address.ToString(); // Ip 地址  
                            }
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                Common.Log.Error(ex.Message);
            }
            return ip;
        }


        /// <summary></summary>  

        /// 显示本机各网卡的详细信息  

        /// <summary></summary>  

        public static void ShowNetworkInterfaceMessage()
        {
           

        }

    }
}
